//
//  PageCollectionVC.swift
//  JTYP
//
//  Created by Groot on 2017/12/9.
//  Copyright © 2017年 inspur. All rights reserved.
//
import MJRefresh
import Alamofire

class PageCollectionVC: BaseCollectionVC {

    let originalPageNumber = 1
    var matches = [Match]()
    
    // 赛事种类
    var channelID = -1
    
    override init(collectionViewLayout layout: UICollectionViewLayout) {
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.collectionView?.register(UINib.init(nibName: "MatchPageCell", bundle: nil), forCellWithReuseIdentifier: MatchPageCell.CellIdentifier)
        
        self.collectionView?.mj_header.beginRefreshing()
    }
    
    func request(start: Int) {
        
        var url = Server.matchList;

        var params = Parameters()
        // 我报名的赛事
        if channelID == -1 {
            params["status"] = 2
            url = Server.enrollList
        }
        // 其他赛事
        else {
            params["status"] = channelID
        }
        
        params["start"] = start
        params["size"] = 10
        params["orderBy"] = "createtime desc"
        
        NetWorkUtil.shareInstance.postForObject(objectType: ListResponse<Match>.self, urlString: url, parameters: params, finishCallBack: ({ [unowned self](obj, error) in
            
            self.bgPlaceholderView.removeFromSuperview()
            self.collectionView?.mj_header.endRefreshing( )
            self.collectionView?.mj_footer.endRefreshing()
            
            guard error == nil else{
                if start == self.originalPageNumber {
                    self.view.addSubview(self.errorPageView)
                    // 加载出错提示
                    self.errorPageView.error(handler: {
                        self.collectionView?.mj_header.beginRefreshing()
                    })
                }else {
                    self.view.makeToast( "加载出错")
                }
                return
            }
            
            guard let result = obj?.data, result.count > 0 else{
                self.collectionView?.mj_footer.endRefreshingWithNoMoreData()
                if start == self.originalPageNumber {
                    self.view.addSubview(self.errorPageView)
                    // 无数据提示
                    self.errorPageView.empty("暂无相关赛事", handler: {
                       self.collectionView?.mj_header.beginRefreshing()
                    })
                }
                return
            }
            if start == 1 {
                self.matches.removeAll()
            }
            self.matches += result
            self.collectionView?.reloadData()
        }))
    }
    @objc override func reload() {
        self.collectionView?.mj_footer.resetNoMoreData()
        request(start: originalPageNumber)
    }
    
    @objc override func loadMore() {
        if matches.count > 0 {
            request(start: matches.count + 1)
        }
    }
    
    // MARK: UICollectionViewDataSource
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return matches.count
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MatchPageCell.CellIdentifier, for: indexPath) as! MatchPageCell
        cell.backgroundColor = .clear
        cell.setData(obj: matches[indexPath.row])
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: self.view.bounds.size.width, height: 80)
    }
}

